home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu374.dms / pu374.adf / AGAtest / gtest2.c < prev    next >
C/C++ Source or Header  |  1992-12-06  |  10KB  |  367 lines

  1. /*
  2.  *  Some SAS/C 6.0 code to show all 2^24 colors available with HAM8
  3.  *  on a sequence of successive screens without ever changing the
  4.  *  64 base color registers after the initial setup.
  5.  *
  6.  *  This code, except the ListRequest () routine, is PD.  Donated to
  7.  *  the public domain by the author, Loren J. Rittle <rittle@comm.mot.com>.
  8.  *  Have fun with it, but please don't claim this work as your own.  As it
  9.  *  is PD, please feel free to take my pieces to your own projects.
  10.  *
  11.  *  ListRequest () is by Willy Langeveld.  You will have to ask Willy
  12.  *  what his license terms are. :-)
  13.  *
  14.  *  Loren J. Rittle
  15.  *  Sun Nov  8 09:52:11 1992
  16.  */
  17.  
  18. /* To allow this to compile with the STRICT ANSI WARNING=ALL options: */
  19. #pragma msg 148 ignore
  20. #pragma msg 149 ignore push
  21. #pragma msg 61 ignore push
  22. #include <proto/dos.h>
  23. #include <proto/exec.h>
  24. #include <proto/intuition.h>
  25. #include <proto/graphics.h>
  26. #include <proto/gadtools.h>
  27. #pragma msg 149 pop
  28. #pragma msg 61 pop
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32.  
  33. /* Thanks to Chris Green: */
  34. LoadRGB32 (struct ViewPort *, unsigned long *table);
  35. #pragma libcall GfxBase LoadRGB32 372 9802
  36.  
  37. /* Let's hope that more entry points are revealed on UseNet to tide us
  38.    over until the real includes are available...
  39.    For example, I'd like the pragma info on the system calls that allow
  40.    me to do double bufferred animation under the OS... :-) */
  41.  
  42. /* Prototype to allow this to compile with WARNING=ALL option: */
  43. int main (int argc, char *argv[]);
  44.  
  45. struct ModeNode
  46. {
  47.   struct Node n;
  48.   ULONG mode;
  49.   UBYTE    name[DISPLAYNAMELEN + 4 + 1];
  50. };
  51.  
  52. static struct TextAttr topaz80 = { "topaz.font", 8, 0, 0 };
  53.  
  54. /*
  55.  *  ListRequest() originally by Willy Langeveld, October 1991.
  56.  *  Changes for this project by myself.
  57.  *  Willy gave me permission to include this code with another
  58.  *  package.  As it is quite useful, I have included it here in
  59.  *  another form.  Thanks Willy!
  60.  */
  61. static struct Node *ListRequest(struct Screen *scr, struct List *list,
  62.                 long x, long y, long width, long height)
  63. {
  64. #define MARGIN 10
  65. #define GADGET_SPACING 5
  66.  
  67.   struct VisualInfo *vi = NULL;
  68.   struct Gadget *glist = NULL, *gad;
  69.   struct NewGadget ng;
  70.   struct Window *w = NULL;
  71.   long top;
  72.   enum {NOTDONE, NOCHANGE, CHANGE} quitflag = NOTDONE;
  73.   ULONG class;
  74.   WORD code, selected;
  75.   struct IntuiMessage *msg;
  76.   struct Node *n = NULL;
  77.  
  78.   if (width < 80)
  79.     width = 80;
  80.   if (height < 70)
  81.     height = 70;
  82.  
  83.   if (scr)
  84.     if (((scr->Flags & SCREENTYPE) == PUBLICSCREEN) ||
  85.         ((scr->Flags & SCREENTYPE) == CUSTOMSCREEN))
  86.       vi = GetVisualInfoA (scr, NULL);
  87.  
  88.   if (!vi)
  89.     {
  90.       scr = LockPubScreen (NULL);
  91.       if (!scr)
  92.     goto cleanup;
  93.  
  94.       vi = GetVisualInfoA (scr, NULL);
  95.       UnlockPubScreen (NULL, scr);
  96.     }
  97.  
  98.   if (!vi)
  99.     goto cleanup;
  100.  
  101.   w = OpenWindowTags (NULL,
  102.                       WA_Left,         (ULONG) x,
  103.                       WA_Top,          (ULONG) y,
  104.                       WA_Width,        (ULONG) width,
  105.                       WA_Height,       (ULONG) height,
  106.                       WA_IDCMP,        (ULONG) LISTVIEWIDCMP | CLOSEWINDOW,
  107.                       WA_Flags,        (ULONG) WINDOWCLOSE  | SMART_REFRESH |
  108.                                                WINDOWDRAG   | WINDOWDEPTH   |
  109.                                                ACTIVATE,
  110.                       WA_Title,        (ULONG) "Select display mode:",
  111.                       WA_CustomScreen, (ULONG) scr,
  112.                       TAG_DONE);
  113.  
  114.   if (!w)
  115.     goto cleanup;
  116.  
  117.   gad = CreateContext(&glist);
  118.  
  119.   if (!gad)
  120.     goto cleanup;
  121.  
  122.   top = w->BorderTop + GADGET_SPACING;
  123.  
  124.   ng.ng_LeftEdge   = MARGIN;
  125.   ng.ng_TopEdge    = top;
  126.   ng.ng_Width      = width - 2 * MARGIN;
  127.   ng.ng_Height     = height - top - GADGET_SPACING;
  128.   ng.ng_GadgetText = NULL;
  129.   ng.ng_GadgetID   = 1;
  130.   ng.ng_Flags      = 0;
  131.   ng.ng_TextAttr   = &topaz80;
  132.   ng.ng_VisualInfo = vi;
  133.   CreateGadget (LISTVIEW_KIND, gad, &ng, GTLV_Labels, list,
  134.                 GTLV_ScrollWidth, 16, TAG_DONE);
  135.  
  136.   AddGList (w, glist, -1, -1, NULL);
  137.   RefreshGList (glist, w, NULL, -1);
  138.   GT_RefreshWindow (w, NULL);
  139.  
  140.   while (quitflag == NOTDONE)
  141.     {
  142.       WaitPort(w->UserPort);
  143.       while (msg = GT_GetIMsg(w->UserPort))
  144.     {
  145.           class  = msg->Class;
  146.           code   = (WORD) msg->Code;
  147.  
  148.           GT_ReplyIMsg (msg);
  149.  
  150.           if (class == GADGETUP)
  151.         {
  152.               selected = code;
  153.               quitflag = CHANGE;
  154.             }
  155.           else if (class == CLOSEWINDOW)
  156.         {
  157.               quitflag = NOCHANGE;
  158.             }
  159.         }
  160.     }
  161.  
  162. cleanup:
  163.   if (w)
  164.     CloseWindow(w);
  165.   if (glist)
  166.     FreeGadgets(glist);
  167.   if (vi)
  168.     FreeVisualInfo(vi);
  169.  
  170.   if ((quitflag == CHANGE) && list)
  171.     {
  172.       n = list->lh_Head;
  173.       while (selected--)
  174.     n = n->ln_Succ;
  175.     }
  176.  
  177.   return(n);
  178. }
  179.  
  180. static ULONG GetUserMode (void)
  181. {
  182.   ULONG mode = INVALID_ID;
  183.   struct List list;
  184.   struct ModeNode *nodep;
  185.  
  186.   NewList (&list);
  187.  
  188.   do
  189.     {
  190.       DisplayInfoHandle dih;
  191.       struct DisplayInfo di;
  192.       struct NameInfo ni;
  193.  
  194.       mode = NextDisplayInfo (mode);
  195.       dih = FindDisplayInfo (mode);
  196.       if (GetDisplayInfoData (dih, (UBYTE *) &di, sizeof di, DTAG_DISP, NULL) &&
  197.       (!di.NotAvailable) && (di.PropertyFlags & DIPF_IS_HAM) &&
  198.       (di.PaletteRange > 65534))
  199.     {
  200.       nodep = malloc (sizeof (struct ModeNode));
  201.       if (!nodep)
  202.         {
  203.           PutStr ("gtest: no more memory\n");
  204.           exit (0);
  205.         }
  206.  
  207.       dih = FindDisplayInfo (mode & ~HAM_KEY);
  208.       if (GetDisplayInfoData (dih, (UBYTE *) &ni, sizeof ni, DTAG_NAME, NULL))
  209.         sprintf (nodep->name, "%s HAM", ni.Name);
  210.       else
  211.         sprintf (nodep->name, "%lx (?) HAM", mode);
  212.       nodep->n.ln_Name = nodep->name;
  213.       nodep->n.ln_Pri = 0;
  214.       nodep->mode = mode;
  215.       AddTail (&list, nodep);
  216.     }
  217.     }
  218.   while (mode != INVALID_ID);
  219.  
  220.   nodep = (struct ModeNode *) ListRequest (NULL, &list, 10, 50, 300, 140);
  221.  
  222.   if (!nodep)
  223.     {
  224.       PutStr ("gtest: no display mode selected\n");
  225.       exit (0);
  226.     }
  227.  
  228.   return nodep->mode;
  229. }
  230.  
  231. static void MText (struct RastPort *rp, ULONG x, ULONG y, char *s)
  232. {
  233.   Move (rp, x, y);
  234.   Text (rp, s, strlen (s));
  235. }
  236.  
  237. static void MDraw (struct RastPort *rp, ULONG x, ULONG y, ULONG tx, ULONG ty)
  238. {
  239.   Move (rp, x, y);
  240.   Draw (rp, tx, ty);
  241. }
  242.  
  243.  
  244. int main (int argc, char *argv[])
  245. {
  246.   UWORD t[] = {0xffff};
  247.   struct Screen *s = OpenScreenTags (NULL, SA_Pens, t, SA_SysFont, 1,
  248.             SA_Title, "AGA HAM8 mode test by Loren J. Rittle",
  249.             SA_DisplayID, GetUserMode (), SA_Depth, 8, TAG_DONE);
  250.  
  251.   if (s)
  252.     {
  253.       struct Window *w = OpenWindowTags (NULL, WA_CustomScreen, s,
  254.         WA_IDCMP, IDCMP_MOUSEBUTTONS, WA_Backdrop, 1,
  255.         WA_Borderless, 1, TAG_DONE);
  256.  
  257.       if (w)
  258.     {
  259.       int i, j, k;
  260.       static ULONG ct[1 + 64 * 3 + 1];
  261.  
  262.       ct[0] = (64 << 16) + 0;
  263.       for (i = 0; i < 4; i++)
  264.         for (j = 0; j < 4; j++)
  265.           for (k = 0; k < 4; k++)
  266.         {
  267.           ct[(k * 16 + j * 4 + i) * 3 + 1] = (i << 30) | (i << 24);
  268.           ct[(k * 16 + j * 4 + i) * 3 + 2] = (j << 30) | (j << 24);
  269.           ct[(k * 16 + j * 4 + i) * 3 + 3] = (k << 30) | (k << 24);
  270.         }
  271.       LoadRGB32 (&(s->ViewPort), ct);
  272.  
  273.       {
  274.         struct RastPort *rp = w->RPort;
  275.         struct TextFont *tf = OpenFont(&topaz80);
  276.         int r, g, b, lastb;
  277.  
  278.         SetFont (rp, tf);
  279.         SetAPen (rp, 3);
  280.         MText (rp, 13, 34, "The colors in this column are all base");
  281.         MText (rp, 13, 42, "registers. Notice that the base");
  282.         MText (rp, 13, 50, "registers never change and that they");
  283.         MText (rp, 13, 58, "are setup in a special way to allow");
  284.         MText (rp, 13, 66, "all 2^24 colors to be seen. These");
  285.         MText (rp, 13, 74, "colors all have their upper 2 bits ==");
  286.         MText (rp, 13, 82, "lower 2 bits to allow you to see how");
  287.         MText (rp, 13, 90, "the lower 2 bits are set.");
  288.         MDraw (rp, 4, 28, 4, 118);
  289.  
  290.         SetAPen (rp, 12);
  291.         MText (rp, 13, 98, "The green line that will appear in");
  292.         MText (rp, 13, 106, "this column is an artifact, ignore.");
  293.         MDraw (rp, 9, 92, 9, 118);
  294.  
  295.         SetAPen (rp, 63);
  296.         MText (rp, 110, 120, "This block of pixels");
  297.         MText (rp, 110, 128, "contains 2^12 unique");
  298.         MText (rp, 110, 136, "colors. 2^12 screens");
  299.         MText (rp, 110, 144, "of these blocks will");
  300.         MText (rp, 110, 152, "show all 2^24 colors.");
  301.         MText (rp, 110, 160, "This should take about");
  302.         MText (rp, 110, 168, "a minute on an A4000");
  303.         MText (rp, 110, 176, "at 60 frames/second.");
  304.         MDraw (rp, 107, 115, 10, 118);
  305.         MDraw (rp, 107, 115, 76, 183);
  306.  
  307.         for (j = 0; j < 4; j++)
  308.           for (k = 0; k < 4; k++)
  309.         for (i = 0; i < 4; i++)
  310.           {
  311.                 SetAPen (rp, j * 16 + k * 4 + i);
  312.                 WritePixel (rp, 4, 120 + i + k * 4 + j * 16);
  313.                 SetAPen (rp, 0xc0);
  314.                 WritePixel (rp, 5, 120 + i + k * 4 + j * 16);
  315.                 SetAPen (rp, 0x40);
  316.                 WritePixel (rp, 6, 120 + i + k * 4 + j * 16);
  317.                 SetAPen (rp, 0x80);
  318.                 WritePixel (rp, 7, 120 + i + k * 4 + j * 16);
  319.                 WritePixel (rp, 8, 120 + i + k * 4 + j * 16);
  320.                 SetAPen (rp, 0xc0 + 0);
  321.                 WritePixel (rp, 9, 120 + i + k * 4 + j * 16);
  322.                 SetAPen (rp, 0x40 + 0);
  323.                 WritePixel (rp, 10, 120 + i + k * 4 + j * 16);
  324.                 for (r = 0; r < 64; r++)
  325.               {
  326.                 SetAPen (rp, 0x80 + r);
  327.                 WritePixel (rp, 10 + r, 120 + i + k * 4 + j * 16);
  328.               }
  329.               }
  330.  
  331.         for (b = 0, lastb = -1; b < 64; b++)
  332.           for (g = 0; g < 64; g++)
  333.         {
  334.           if (CheckSignal (SIGBREAKF_CTRL_C | (1 << w->UserPort->mp_SigBit)))
  335.             goto out;
  336.  
  337.           WaitTOF ();
  338.  
  339.           SetAPen (rp, 0xc0 + g);
  340.           MDraw (rp, 9, 120, 9, 183);
  341.           if (b != lastb)
  342.             {
  343.               SetAPen (rp, 0x40 + b);
  344.               MDraw (rp, 10, 120, 10, 183);
  345.               lastb = b;
  346.             }
  347.         }
  348.  
  349.         MText (rp, 110, 190, "Done. Click mouse button.");
  350.         CloseFont (tf);
  351.       }
  352.  
  353.       Wait (SIGBREAKF_CTRL_C | (1 << w->UserPort->mp_SigBit));
  354.  
  355.     out:
  356.       CloseWindow (w);
  357.     }
  358.       else
  359.     PutStr ("gtest: error opening window\n");
  360.  
  361.       CloseScreen (s);
  362.     }
  363.   else
  364.     PutStr ("gtest: error opening screen\n");
  365.   return (0);
  366. }
  367.